package com.yst.feign1.common;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.yst.feign1.common.ErrorCode;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.experimental.Accessors;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @date 2019年6月24日14:30:34
 */
@Data
@Accessors(chain = true)
public class ApiResponse<T> implements Serializable {

    private static final long serialVersionUID = 9148152608850849683L;

    @ApiModelProperty(value = "返回代码")
    private int code = ErrorCode.NO_ERROR.getCode();

    @ApiModelProperty(value = "返回信息")
    private String msg = ErrorCode.NO_ERROR.getMessage();

    @ApiModelProperty(value = "返回时间")
    private String timestamp = getNowDate();

    @ApiModelProperty(value = "返回数据")

    @JsonInclude(Include.NON_NULL)
    private T result;

    private String traceId;

    public ApiResponse() {
        super();
    }

    public ApiResponse(T t) {
        this.result = t;
    }

    public ApiResponse(ErrorCode protocolErrorCode) {
        this.code = protocolErrorCode.getCode();
        this.msg = protocolErrorCode.getMessage();
    }

    public ApiResponse(ErrorCode protocolErrorCode, String msg) {
        this.code = protocolErrorCode.getCode();
        this.msg = msg;
    }

    private static String getNowDate() {
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return formatter.format(currentTime);
    }
}